﻿using SuperJoe.Framwork.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WWX.Admin.BLL;
using WWX.Admin.Web.Models;
using WWX.Dto.Search;

namespace WWX.Admin.Web.Controllers
{
    public class MemberController : AuthorizationController
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult List(MemberAdminSearch search, int limit, int pageIndex = 0)
        {
            int results = 0;
            var list = new MemberBLL().GetPageList(search, ++pageIndex, limit, out results);
            return this.Json(new { results, rows = list.Select(l => new { id = l.Id, account = l.Account, nickName = l.NickName, trueName = l.TrueName.ToString(), phone = l.Phone, isActive = l.IsActive, registerDate = l.RegisterDate == DateTime.MinValue ? "-" : l.RegisterDate.ToString(), address = l.Address, cash = l.Cash, zcash = l.ZCash, remark = l.Remark }) }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(MemberVM vm)
        {
            vm.Validate();
            var bll = new MemberBLL();
            var member = vm.ToMember();
            if (member.Id.IsNotNullOrWhiteSpace())
            {
                bll.Update(member);
            }
            else
            {
                bll.Add(member);
            }

            return this.CreateRichJsonResult();
        }
    }
}